Stored Procedures [dbo].[BAEGetSuffixLabels]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
SQL Script
CREATE procedure [dbo].[BAEGetSuffixLabels] as
    DECLARE @suffixDescription varchar(255)
    DECLARE @suffix table(code varchar(60), description varchar(255))

    DECLARE SuffixCursor CURSOR -- Declare the cursor and get all of the distinct description values
    READ_ONLY
    FOR
    SELECT DISTINCT DESCRIPTION
    FROM Gen_Tables
    WHERE TABLE_NAME = 'SUFFIX' AND (LEN(RTRIM(LTRIM(DESCRIPTION))) > 0)

    OPEN     SuffixCursor    -- Open the cursor

    FETCH NEXT FROM SuffixCursor INTO @suffixDescription    -- Get the first record
    WHILE (@@fetch_status <> -1)    -- Loop through results
    BEGIN
        
        -- Select the first code in the table based on the description and store that in the table variable @suffix
        INSERT INTO @suffix(code, description)
        SELECT TOP 1 CODE, @suffixDescription
        FROM Gen_Tables g
        WHERE TABLE_NAME = 'SUFFIX' and DESCRIPTION = @suffixDescription

        FETCH NEXT FROM SuffixCursor INTO @suffixDescription    --Get the next record/description
    END

    -- get rid of the cursor
    CLOSE SuffixCursor
    DEALLOCATE SuffixCursor

    -- Return all of the values from the suffix table variable which will include distinct descriptions and the associated codes
    SELECT * FROM @suffix
---------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
Uses